//
//  LogInViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/17.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "LogInViewController.h"
#import "RegisterViewController.h"
#import "ForgetPassWordViewController.h"

#define LogInViewUIWidth self.view.width - Margin * 4
@interface LogInViewController ()<UITextFieldDelegate>

@property (nonatomic,strong)UITextField *nameNumberTextField;

@property (nonatomic,strong)NSString *nameNumberString;

@property (nonatomic,strong)UITextField *passWordTextField;

@property (nonatomic,strong)NSMutableDictionary *logInDic;

@end

@implementation LogInViewController

- (NSMutableDictionary *)logInDic {
    if (!_logInDic) {
        _logInDic = [NSMutableDictionary dictionary];
    }
    return _logInDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpNavigationBar];
    
    [self loadLogInViewUI];
    
}


- (void)setUpNavigationBar {
    
    self.navigationController.navigationBar.barTintColor = DEFAULT_COLOR;
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"jiantou"] highImage:[UIImage imageNamed:@"jiantou"] target:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside title:nil];
    self.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setText:@"登录"];
    customLab.font = [UIFont boldSystemFontOfSize:18];
    self.navigationItem.titleView = customLab;
    
}


/**
 *  返回按钮点击方法
 */
- (void)backToPre {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadLogInViewUI {
    
    CGFloat Margin;
    
    if (iPhone5 && iPhone6) {
        Margin = 8;
    } else {
        Margin = 12;
    }
    
    UIImageView *LoGoImageView = [[UIImageView alloc] init];
    LoGoImageView.image = [UIImage imageNamed:@"tubiao108"];
    [self.view addSubview:LoGoImageView];
    
    UITextField *nameNumberTextField = [[UITextField alloc] init];
    nameNumberTextField.borderStyle = UITextBorderStyleNone;
    nameNumberTextField.placeholder = @" 昵称 / 手机 / 邮箱";
    nameNumberTextField.clearButtonMode = UITextFieldViewModeAlways;
    [nameNumberTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.nameNumberTextField = nameNumberTextField;
    [self.view addSubview:nameNumberTextField];
    nameNumberTextField.font = [UIFont systemFontOfSize:15];
    self.nameNumberTextField.delegate = self;
    
    
    UIView *numberButtom = [[UIView alloc] init];
    numberButtom.backgroundColor = [UIColor blackColor];
    numberButtom.alpha = 0.1;
    [self.view addSubview:numberButtom];
    
    UITextField *passWord = [[UITextField alloc] init];
    passWord.borderStyle = UITextBorderStyleNone;
    passWord.placeholder = @" 密码";
    passWord.font = [UIFont systemFontOfSize:15];
    passWord.secureTextEntry = YES;
    passWord.clearButtonMode = UITextFieldViewModeAlways;
    self.passWordTextField = passWord;
    self.passWordTextField.delegate = self;
    [self.view addSubview:passWord];
    
    UIView *passWordButtom = [[UIView alloc] init];
    passWordButtom.backgroundColor = [UIColor blackColor];
    passWordButtom.alpha = 0.1;
    [self.view addSubview:passWordButtom];
    
    UIButton *logInButton = [[UIButton alloc] init];
    logInButton.backgroundColor = DEFAULT_COLOR;
    logInButton.layer.cornerRadius = 3;
    logInButton.layer.masksToBounds = YES;
    logInButton.titleLabel.font = FONT(15);
    [logInButton setTitle:@"登录" forState:UIControlStateNormal];
    [logInButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logInButton addTarget:self action:@selector(didClickLogInButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logInButton];
    
    UIButton *registerButton = [[UIButton alloc] init];
    registerButton.titleLabel.font = FONT(15);
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    registerButton.layer.cornerRadius = 3;
    registerButton.layer.masksToBounds = YES;
    registerButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    registerButton.layer.borderWidth = 0.5;
    [registerButton addTarget:self action:@selector(didClickRegisterButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerButton];
    
    UIButton *forgetPassword = [[UIButton alloc] init];
    [forgetPassword setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [forgetPassword setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    forgetPassword.titleLabel.textAlignment = NSTextAlignmentRight;
    forgetPassword.titleLabel.font = [UIFont systemFontOfSize:13];
    [forgetPassword addTarget:self action:@selector(didClickforgetPasswordButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetPassword];
    
    
    [LoGoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(HEIGHT/8);
        make.width.mas_equalTo(self.view.width/5);
        make.height.mas_equalTo(self.view.width/5);
        
    }];
    
    [nameNumberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(LoGoImageView.mas_centerX);
        make.top.equalTo(LoGoImageView.mas_bottom).with.offset(Margin*2);
        make.width.mas_equalTo(LogInViewUIWidth);
        make.height.mas_equalTo(30);
    }];
    
    [numberButtom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(nameNumberTextField.mas_bottom).with.offset(5);
        make.width.mas_equalTo(LogInViewUIWidth);
        make.height.mas_equalTo(1);
    }];
    
    [passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(numberButtom.mas_centerX);
        make.top.equalTo(numberButtom.mas_bottom).with.offset(Margin);
        make.width.mas_equalTo(LogInViewUIWidth);
        make.height.mas_equalTo(30);
    }];
    
    [passWordButtom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(passWord.mas_bottom).with.offset(5);
        make.width.mas_equalTo(LogInViewUIWidth);
        make.height.mas_equalTo(1);
    }];
    
    [logInButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(passWordButtom.mas_bottom).with.offset(Margin * 3);
        make.width.mas_equalTo(LogInViewUIWidth);
        make.height.mas_equalTo(40);
    }];
    
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        
        if (iPhone5 && iPhone6) {
            make.top.equalTo(logInButton.mas_bottom).with.offset(Margin * 2);
        } else {
            make.top.equalTo(logInButton.mas_bottom).with.offset(Margin + 4);
        }
        
        make.width.mas_equalTo(LogInViewUIWidth);
        make.height.mas_equalTo(40);
    }];
    
    [forgetPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).with.offset(-Margin);
        make.top.equalTo(registerButton.mas_bottom).with.offset(Margin);
        make.width.mas_equalTo(self.view.Height/10 + Margin);
        make.height.mas_equalTo(25);
    }];
}

/**
 *  登录点击方法
 */
- (void)didClickLogInButton {
    
    [self resignTextField];
    
    if (self.passWordTextField.text.length != 0 && self.nameNumberTextField.text.length != 0) {
        NSDictionary *params = @{
                                 @"username":self.nameNumberTextField.text,
                                 @"password":self.passWordTextField.text
                                 };
        
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"AppUser/CheckLogin" completeBlock:^(id object) {
            
            NSLog(@"%@",object);
            
            self.logInDic = object;
            
//            [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"登录成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
//                self.logInDic = [object objectForKey:@"data"];
//                
//                [self logInSuccess];
//            }];
            
            [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"登录成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
                [self backToPre];
                
            } andBackDataBlock:^(id object) {
                self.logInDic = [object objectForKey:@"data"];
                
                NSLog(@"%@",self.logInDic);
                
                [ReadLoginData savelogindataToLocal:self.logInDic];
            }];
            
        }];
        
    } else {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请输入账号或密码" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        
        [self.view addSubview:hud];
    }
    
}

/**
 *  注册按钮点击方法
 */
- (void)didClickRegisterButton {
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushForm:self.navigationController pushToAndHidden:registerVC];
}

/**
 *  忘记密码按钮
 */
- (void)didClickforgetPasswordButton {

//    MBProgressHUD *hud = [[MBProgressHUD alloc] init];
//    [hud show:YES];
//    hud.labelText = @"正在加载中";
//    [self.view addSubview:hud];
    
//    [MBProgressHUD show:self.view andMessage:@""];
//    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//        
//    });

    ForgetPassWordViewController *forget = [[ForgetPassWordViewController alloc] init];
    [self.navigationController pushForm:self.navigationController pushToAndHidden:forget];
    
    
}

/**
 *  手机号码输入设置
 */
- (void)textFieldDidChange:(UITextField *)textField
{
    
    if (textField == self.nameNumberTextField) {
        if (textField.text.length >=11) {
            textField.text = [textField.text substringToIndex:11];
        }
        if (textField.text.length >10) {
//            NSLog(@"%@",textField.text);
            self.nameNumberString = textField.text;
        } else {
            //            self.belongLabel.text = BelongLabelText;
        }
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s",__func__);
    [self resignTextField];
}

- (void)resignTextField {
    [self.nameNumberTextField resignFirstResponder];
    [self.passWordTextField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == self.nameNumberTextField) {
        [self.passWordTextField becomeFirstResponder];
    }
    
    if (textField == self.passWordTextField) {
        [self didClickLogInButton];
    }
    
    return YES;
}

/**
 *  登录成功后操作
 */
- (void)logInSuccess {
    [ReadLoginData savelogindataToLocal:self.logInDic];
    
    [self backToPre];
}


@end
